<form method="POST" onSubmit="onSubmit(); return false;">
    <div class="row-fluid">
        <div class="span2">
            <label for="ip-format">{{ _( "Export format" ) }}</label>
        </div> <!-- .span2 -->
        <div class="span10">
            <select id="ip-format">
                <option value="1" selected>IP|IP|IP (Gogo-Tester/Goagent{{ _( " format" ) }})</option>
                <option value="2">{{ _( "New line delimiter" ) }}</option>
            </select>
        </div> <!-- .span10 -->
    </div> <!-- .row-fluid -->
    <div class="row-fluid">
        <div class="span2">
            <label for="ip-list">{{ _( "IP address list" ) }}</label>
        </div> <!-- .span2 -->
        <div class="span10">
            <textarea id="ip-list" rows="10"></textarea>
        </div> <!-- .span10 -->
    </div> <!-- .row-fluid -->
    <div class="row-fluid">
        <div class="span12">
            <button class="btn btn-primary btn-block" type="submit">{{ _( "Run" ) }}</button>
        </div> <!-- .span12 -->
    </div> <!-- .row-fluid -->
</form> <!-- #import-export-ip -->

<script type="text/javascript">
    function doExportAction(ipFormat) {
        $.ajax({
            type: 'POST',
            url: '/module/gae_proxy/control/importip?cmd=exportip',
            dataType: 'JSON',
            success: function(result) {
                var ipList = result['res'];

                tip('{{ _("Succeeded exporting ") }}' + ipList.split('|').length + '{{ _(" IP addresses.") }}', 'success');

                ipList = conversionFormat('1', ipFormat, ipList);
                $('#ip-list').val(ipList);
            },
            error: function() {
                tip('{{ _("Failed exporting the IP addresses.") }}', 'error');
            }
        });
    }
</script>
<script type="text/javascript">
    function conversionFormat(originalFormat, targetFormat, sourceData) {
        if( originalFormat == targetFormat ) {
            return sourceData;
        }

        var ipList;
        switch(originalFormat) { //由源数据转换为中间格式
        case '1': //IP|IP|IP
            ipList = sourceData.split("|");
            break;
        case '2': //换行分割
            ipList = sourceData.split("\n");
            break;
        case '3': //智能正则匹配
            var tmp = ' ' + sourceData;
            var regex = /(?!\d)[^\d](([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])(\.([1-9]?\d|1\d{2}|2[0-4]\d|25[0-5])){3}(?!\d))/gm;

            var regexResult;
            ipList = [];
            while(true) {
                regexResult = regex.exec(tmp);

                if(regexResult === null) {
                    break;
                }

                ipList.push(regexResult[1]);
            }
            break;
        }

        var result;
        switch(targetFormat) { // convert from the interim format to the destination format由中间格式转换为目标格式
        case '1': //IP|IP|IP
            result = '';
            for(var id in ipList) {
                result += ipList[id] + '|';
            }
            result = result.substr(0, result.length - 1);
            break;
        case '2': //New line delimiter换行分割
            result = '';
            for(var id in ipList) {
                result += ipList[id] + '\n';
            }
            break;
        }

        return result;
    }
</script>

<script type="text/javascript">
    function onSubmit() {
        var ipFormat = $('#ip-format').val();

        return doExportAction(ipFormat);
    }
</script>
